﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SeparateProjects
{
    public partial class Pro_User_PersonalInformation : Form
    {
        SqlConnection coon = new SqlConnection(@"Data Source =DESKTOP-28VSNS5 ;Initial Catalog = Training_Project;
                                                Uid = Lv;Pwd = 123456");
        #region 获取登录账号
        /// <summary>
        /// 传参获取数据
        /// </summary>
        /// <param name="Number"></param>
        public Pro_User_PersonalInformation( string Number)
        {
            InitializeComponent();
            this.txt_Sid.Text = Number;
            this.txt_Sid.Enabled = false;
        }
        #endregion
        #region  通过学号获取名字
        /// <summary>
        /// 获取名字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pro_User_PersonalInformation_Load(object sender, EventArgs e)
        {
            var Sid = this.txt_Sid.Text;
            try
            {
                coon.Open();
                var sql = string.Format(@"select S.Stu_StuInfo_Name 
                                        from Stu_StuInfo S
                                        inner join Stu_Login T on(T.Sid =S.Sid)
                                        where T.UserName = '{0}'", Sid);
                var comm = new SqlCommand(sql, coon);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    var UserName = dr["Stu_StuInfo_Name"];
                    this.txt_Name.Text = UserName.ToString();
                    this.txt_Name.Enabled = false;
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常，请联系管理员" + ex.Message);
            }
            finally
            {
                coon.Close();
            }
        }
        #endregion
        #region  点击查询信息
        /// <summary>
        /// 点击查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Search_Click(object sender, EventArgs e)
        {
            var UserName = this.txt_Name.Text;
            var sql = string.Format(@"select S.Stu_StuInfo_Name,SE.Sex_Name,S.Stu_StuInfo_Age,G.Grede_Name,
                                    S.Stu_StuInfo_Card,S.Stu_StuInfo_Address
                                    from Stu_StuInfo S
                                    inner join Stu_Login D on(S.Sid = D.Sid)
                                    inner join Sex Se on(Se.Stu_StuInfo_Sex = S.Stu_StuInfo_Sex)
                                    inner join Grade G on(G.Stu_StuInfo_Grade = S.Stu_StuInfo_Grade)
                                    where S.Stu_StuInfo_Name = '{0}'",UserName);
            DataSet da = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(sql,coon);
            adp.Fill(da);
            DataTable dt = da.Tables[0];
            this.Infomation_DataGridView1.DataSource = dt;
            //是否允许自动创建列
            this.Infomation_DataGridView1.AutoGenerateColumns = false;
            //不允许出现空行
            this.Infomation_DataGridView1.AllowUserToAddRows = false;
            //整行选中
            this.Infomation_DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //是否允许多选
            this.Infomation_DataGridView1.MultiSelect = false;
        }
        #endregion
    }
}
